package top.lyjwn.ajui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;


import androidx.annotation.NonNull;
import top.lyjwn.ajui.R;

/**
 * 底部弹窗
 *
 * @author luoyujie
 */
public class BottomDialog extends Dialog {

    private Context context;
    private int layoutResId;

    public BottomDialog(@NonNull Context context, int layoutResId, Build build) {
        super(context, R.style.bottom_dialog_theme);
        this.context = context;
        this.layoutResId = layoutResId;
        View view = View.inflate(context, layoutResId, null);
        setContentView(view);
        setBuild(build);
    }
    public BottomDialog(@NonNull Context context, int layoutResId) {
        super(context, R.style.bottom_dialog_theme);
        this.layoutResId = layoutResId;
        this.context = context;

    }

   public void setBuild(Build build){

       View view = View.inflate(context, layoutResId, null);
       setContentView(view);
       Window window = getWindow();
       //设置弹出位置
       window.setGravity(Gravity.BOTTOM);
       //设置弹出动画
       window.setWindowAnimations(R.style.bottom_dialog_anim_style);
       //设置对话框大小
       window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       View cancel = view.findViewById(R.id.cancel);
       if (cancel != null) {
           cancel.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   dismiss();
               }
           });
       }
       build.init(view,this);
    }


    public interface Build {
        void init(View v, BottomDialog bd);
    }
}
